<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Add_truck
 *
 * @author admin
 */
class add_truck extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('truck_model');
        $this->load->model('catalog');
    }

    public function index() {
        $this->_set_form();
        $this->load->view('truck/add_truck', $this->data);
    }

    private function _set_form() {
        $this->data['txt_license_plate'] = form_input('txtLicensePlate', set_value('txtLicensePlate'), 'class="form-control"');
        $this->data['cmb_brand'] = form_dropdown('cmbBrand', $this->catalog->list_truck_brand(), set_value('cmbBrand'), 'class="form-control" id="cmbBrand"');
        $this->data['cmb_model'] = form_dropdown('cmbModel', $this->catalog->list_truck_model($this->input->post('cmbBrand')), set_value('cmbModel'), 'class="form-control" id="cmbModel"');
        $this->data['txt_moter_code'] = form_input('txtMoterCode', set_value('txtMoterCode'), 'class="form-control"');
        $this->data['txt_mile_number'] = form_input('txtMileNumber', set_value('txtMileNumber'), 'class="form-control"');
        $this->data['cmb_driver_name'] = form_dropdown('cmbDriverName', $this->truck_model->get_list_driver(), set_value('cmbDriverName'), 'class="form-control"');
        $this->_set_error();
    }

    private function _set_error() {
        $this->data['err_license_plate'] = form_error('txtLicensePlate', '<div class="error">', '</div>');
        $this->data['err_brand'] = form_error('cmbBrand', '<div class="error">', '</div>');
        $this->data['err_model'] = form_error('cmbModel', '<div class="error">', '</div>');
    }

    public function validate() {
        $this->form_validation->set_rules('txtLicensePlate', 'License Plate', 'required');
        $this->form_validation->set_rules('cmbBrand', 'Brand', 'required');
        $this->form_validation->set_rules('cmbModel', 'Model', 'required');
        $this->form_validation->set_rules('txtMoterCode', 'Moter Code', '');
        $this->form_validation->set_rules('txtMileNumber', 'Mile Number', '');
        $this->form_validation->set_rules('cmbDriverName', 'Driver', '');

        $license_code = $this->input->post('txtLicensePlate');
        $brand_id = $this->input->post('cmbBrand');
        $model_id = $this->input->post('cmbModel');
        $moter_code = $this->input->post('txtMoterCode');
        $mile_number = $this->input->post('txtMileNumber');
        $driver_id = $this->input->post('cmbDriverName');

        $utility = new Config_form();

        $config['upload_path'] = './uploads/trucks/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '3072';
        $config['overwrite'] = FALSE;
        $config['encrypt_name'] = TRUE;

        $this->load->library('upload', $config);
        $count_upload = $this->input->post('count_upload');

        if ($this->form_validation->run() == FALSE) {
            $this->_set_form();
            $this->load->view('truck/add_truck', $this->data);
        } else {
            $result = $this->truck_model->save_data($license_code, $brand_id, $model_id, $moter_code, $mile_number, $driver_id);
            if ($result) {
                $truck = $this->truck_model->get_truck_by_license($license_code);
                
                $count = 1;
                for ($i = 0; $i < $count_upload; $i++) {
                    if ($_FILES['uploadFile' . $i]['name'] != NULL) {
                        if ($this->upload->do_upload('uploadFile' . $i)) {
                            $upload_data = $this->upload->data();
                            $this->truck_model->save_image($truck[0]->id, $count, $upload_data['file_name']);
                            $count++;
                        }
                    }
                }
                echo $utility->form_alert_success('บันทึกเสร็จเรียบร้อย', 'truck/home');
                exit();
            } else {
                $this->_set_form();
                $this->load->view('truck/add_truck', $this->data);
            }
        }
    }

    public function get_model() {
        $id = $this->input->post('id');
        $list_model = $this->catalog->list_truck_model($id);
        $array = array();
        $array['list_model'] = $list_model;
        $array['length'] = count($list_model);
        echo json_encode($array);
    }

}
